﻿#include "dealexcelfile.h"
#include <QSqlError>
#include <QFileInfo>
#include <QMessageBox>
DealExcelFile::DealExcelFile(QObject *parent) :
    QObject(parent)
{
    xlsOpenFlag = false;
    dbOpenFlag  = false;
    hasExistFile = false;
    IsXlsFile   = false;
    IsDbFile    = false;
    m_bOpenSuccess = false;
    sqlite  = new multideal();//一个数据库操作接口
    excelFile = new ExcelEngine;
    //model = new QSqlTableModel(this);不能放在这里  否则TableView为空
    //tableview = new QTableView(this);
    //tableview->hide();
}

DealExcelFile::~DealExcelFile()
{
    excelFile->Close();
}
DealExcelFile* getDealExecl()
{
    static DealExcelFile execl;
    return &execl;
}
bool DealExcelFile::OpenExcelFile()
{
    if(!m_bOpenSuccess)
    {
        return  false;
    }
    if(!excelFile->Open())
    {
        return false;
    }
    return true;
}

void DealExcelFile::Close()
{
    excelFile->Close();
}

void DealExcelFile::Init()
{
    QFile file("./RoadInfo.xls");
    if(!file.exists())
    {
        m_bOpenSuccess = false;
        QMessageBox::warning(0,"提示",tr("<font size='2' face='宋体'>路段表不存在</font>"),"确定");
        return;
    }
    if(!file.open(QIODevice::ReadOnly))
    {
        m_bOpenSuccess = false;
        return;
    }
    QFileInfo tmp(file);
    excelFile->SetExcelFile(tmp.absoluteFilePath());
    m_bOpenSuccess = true;
    //excelFile->Init();
}
void DealExcelFile::createTable(int row, int col)
{
    //excelTable = new QTableWidget(row,col,this);
    excelTable = new QTableWidget(row,col);
    excelTable->setGeometry(0,20,400,280);
    excelFile->ReadDataToTable(excelTable);
    excelTable->show();
    xlsOpenFlag = true;
}

bool DealExcelFile::createTree(int row, int col,QTreeWidget* tree)
{
    if(!OpenExcelFile())
    {
        return false;
    }
    excelTree = tree;
    excelTree->setHeaderLabel("路段信息");
    excelFile->ReadDataToTree(excelTree);
    excelTree->show();
    //excelTree->expandAll();
    excelTree->collapseAll();
    return true;
}

void DealExcelFile::SetPointStatue(const QString ip, const int statue)
{
    QMutexLocker lock(&m_mutex);
    //将设备状态存在map中,用于树初始化时设置树状态
    excelFile->SetIPAndStatue(ip,statue);
    excelFile->SetPointStatue(ip,statue);
}

//void DealExcelFile::SetIPAndStatue(const QString ip, const int statue)
//{
//    QMutexLocker lock(&m_mutex);
//    excelFile->SetIPAndStatue(ip,statue);
//}

bool DealExcelFile::GetIPByItem(QTreeWidgetItem *item, QString &ip)
{
    QMutexLocker lock(&m_mutex);
    return excelFile->GetItemLaneIP(item,ip);
}

bool DealExcelFile::GetNameByIP(const QString ip, QString &name,QString &firstName)
{
    QMutexLocker lock(&m_mutex);
    return excelFile->GetItemLaneName(ip,name,firstName);
}


